package jsu.xnm.evelyn.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import jakarta.annotation.Resource;
import jsu.xnm.evelyn.bean.Category;
import jsu.xnm.evelyn.service.CategoryService;
import jsu.xnm.evelyn.util.BaseContext;
import jsu.xnm.evelyn.util.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author xnm
 * @version 1.0
 */
@RestController
@Slf4j
@RequestMapping("/category")
public class CategoryController {

    @Resource
    private CategoryService categoryService;

    /**
     * 新增分类
     * @param category
     * @return
     */
    @PostMapping("/save")
    public Result save(@RequestBody Category category){
        //输出category
        log.info("category:{}",category);
        BaseContext.setCurrentId(3);//todo
        categoryService.save(category);
        return Result.success("新增分类成功!");
    }

    @GetMapping("/page")
    public Result page(@RequestParam(name = "pageNo",defaultValue = "1") Integer pageNo,
                       @RequestParam(name = "pageSize",defaultValue = "10") Integer pageSize){
        LambdaQueryWrapper<Category> queryWrapper = Wrappers.lambdaQuery();
        //添加排序条件（根据表中的sort排序）
        queryWrapper.orderByAsc(Category::getSort);

        Page<Category> page = categoryService.page(new Page<>(pageNo, pageSize), queryWrapper);
        return Result.success(page);
    }

    @PutMapping("/update")
    public Result update(@RequestBody Category category){
        log.info("修改分类，category为：{}",category);
        BaseContext.setCurrentId(4);//todo
        categoryService.updateById(category);
        return Result.success("修改分类成功!");
    }

    @GetMapping("/getById/{id}")
    public Result getById(@PathVariable("id") Long id){
        //todo 前端从后端获取数据后返回并填充表单后再进行修改操作
        log.info("根据id查询分类，id为：{}",id);
        Category category = categoryService.getById(id);
        if(category != null){
            return Result.success(category);
        }
        return Result.error("400","无该分类信息!");
    }

    /**
     * 获取所有分类信息
     * @param type  1菜品分类 2套餐分类
     * @return
     */
    @GetMapping("/getAllCategories/{type}")
    public Result getAllCategories(@PathVariable("type") Integer type){
        log.info("根据type查询分类，type为：{}",type);

        LambdaQueryWrapper<Category> queryWrapper = Wrappers.lambdaQuery();
        if(type != null){
            queryWrapper.eq(Category::getType, type);
        }
        queryWrapper.orderByAsc(Category::getSort);

        List<Category> list = categoryService.list(queryWrapper);
        return Result.success(list);
    }

    /**
     * 删除分类
     * @param id
     * @return
     */
    @DeleteMapping("/del")
    public Result delete(Long id){
        log.info("删除分类，id为：{}",id);
        categoryService.remove(id);
        return Result.success("删除分类成功!");
    }
}
